function feature_list = detect_corners (A_img,n) 
% n is the desired number of features
percentage_overall=20/100;
s=size(A_img);  %s = [rows columns]
pix_per_region=s(1)*s(2)/36;

n_hor_region=round(s(2)/sqrt(pix_per_region));
n_vert_region=round(s(1)/sqrt(pix_per_region));

region_w=s(2)/n_hor_region;
region_h=s(1)/n_vert_region;

features_per_region=round(n*(1-percentage_overall)/36);
strongest_features_overall = n - 36*features_per_region;

min_threshold_in_percentage = 1/10; % to be a feature, its value has to be over this percentage among all features in the image

%input image
%A_img = imread(img_file);
img_h = s(1);
img_w = s(2);
if isrgb(A_img)
    A = double(rgb2gray(A_img));
else
    A  = double(A_img);%
end

%parameters for the gaussian blur
gaussian_size = 7; sigma = .7;

%feature window calculation
%derivatives
del_A_1 = conv2(A,[-1,1],'same') ;
del_A_2 = conv2(A,[-1;1],'same') ;
del_A_1_1 = del_A_1 .* del_A_1;
del_A_2_2 = del_A_2 .* del_A_2;
del_A_1_2 = del_A_1 .* del_A_2;
%gauss blur
gaussian = fspecial('gaussian',gaussian_size,sigma);
matrix_1_1 = conv2(del_A_1_1,gaussian,'same');
matrix_2_2 = conv2(del_A_2_2,gaussian,'same');
matrix_1_2 = conv2(del_A_1_2,gaussian,'same');
to_i = img_h+1-gaussian_size;
to_j = img_w+1-gaussian_size;
k = 0.04;
R = zeros(img_h,img_w);
length_L = to_i*to_j;
L = zeros(length_L,3);
ind_L = 1;
for i= 1:to_i
    for j=1:to_j
        M = [matrix_1_1(i,j),matrix_1_2(i,j);
            matrix_1_2(i,j),matrix_2_2(i,j)];
        tM = trace(M);
        dM = det(M);
        R(i,j) = dM-k*tM^2; %min(abs(eig(M)));
    end
end
for i= 2:to_i-1
    for j=2:to_j-1
%        R(i,j) = dM-k*tM^2; %min(abs(eig(M)));
        if (R(i,j)==max(max(R(i-1:i+1,j-1:j+1))))%if the current R value is the maximum of the 8-connected
            L(ind_L,1) = R(i,j);
            L(ind_L,2) = round(i);
            L(ind_L,3) = round(j);
            ind_L = ind_L + 1;%thus, L will contain the values of R and the coordinates
        end
    end
end


%find features
feature_index=1;
is_not_a_feature = ones(img_h,img_w);
[~, sorted_ind] = sort(L,1);
min_threshold = L(sorted_ind(ceil(length_L*min_threshold_in_percentage)),1);
region = zeros(ceil(img_h/region_h), ceil(img_w/region_w));
total_num_region = ceil(img_h/region_h)*ceil(img_w/region_w)*features_per_region;
counter_total_region = 0;
%TODO: following can be shortened to only do it for L(:,1) > thresh
for i = 1:length_L
    elem = sorted_ind(length_L-i+1);
    m = L(elem,2);
    n = L(elem,3);

    if i <= strongest_features_overall %if its one of the strongest features
        is_not_a_feature(m,n) = 0; %it is a feature
        feature_list(feature_index,1:2)= [m n];
        feature_index = feature_index + 1;%put in this list's first row the index of the feature ... also the coords of the feature
    else 
        p = ceil(m/region_h);
        q = ceil(n/region_w);%puts the current coord into the correct region 'bin'
        if p == 0 || q == 0 
            continue;
        end%here we go hrough the image's regions using p and q, the regions are represented in 'region'
        if region(p,q) < features_per_region
           region(p,q) = region(p,q) +1;
            if L(elem,1) > min_threshold
                is_not_a_feature(m,n) = 0;
                feature_list(feature_index,1:2)= [m n];%add to the feature list if the L's are stronger
                feature_index = feature_index + 1;
            end    
            counter_total_region = counter_total_region +1;
            if counter_total_region >= total_num_region
                break;
            end
        end
    end
end